package com.css.crm.dao.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.css.crm.dao.SystemDao;
import com.css.crm.exception.SystemException;
import com.css.crm.message.SystemErrorMessage;
import com.css.crm.utility.ConstantUtil;
import com.css.crm.vo.AbstractObjectPermission;
import com.css.crm.vo.DiscountCodeVo;
import com.css.crm.vo.DepartmentVo;
import com.css.crm.vo.PlaceVo;
import com.css.crm.vo.StaticReferenceVo;
import com.css.crm.vo.MenuMap;
import com.css.crm.vo.LdapVo;
import com.css.crm.vo.SmUserVo;
import com.css.crm.vo.TitleVo;
import com.css.crm.vo.SystemMenu;
import com.css.crm.vo.User;

/**
 * Implementation class for SystemDao to handles calls from SystemService
 */
public class SystemDaoImpl extends BaseDaoImpl implements SystemDao {

	public SystemDaoImpl() {
	}

	/*
	 * (non-Javadoc)
	 * @see com.css.crm.dao.SystemDao#getApplTopMenu()
	 */
	public List<SystemMenu> getSystemMenu() {
		SqlSession session = getMyBatisSession();
		return session.selectList("system.getSystemMenu");
	}

	/*
	 * (non-Javadoc)
	 * @see com.css.crm.dao.SystemDao#getCrm1MenuMap()
	 */
	public MenuMap getCrmMenuMap() {
		SqlSession session = getMyBatisSession();
		return (MenuMap) session.selectList("system.getCrmMenuMap").get(0);
	}

	/*
	 * (non-Javadoc)
	 * @see com.css.crm.dao.SystemDao#getApplAccount(java.lang.String, java.lang.String)
	 */
	public List<User> getApplAccount(String ldapId, String applId) {
		HashMap<String, String> parameters = new HashMap<String, String>();
		parameters.put("ldapId", ldapId);
		parameters.put("applId", applId);
		SqlSession session = getMyBatisSession();
		return session.selectList("system.getApplAccount", parameters);
	}

	/*
	 * (non-Javadoc)
	 * @see com.css.crm.dao.SystemDao#getAllObjectPermission(java.lang.String, java.lang.String)
	 */
	public List<? extends AbstractObjectPermission> getAllObjectPermission(String accountId, String applId) {
		HashMap<String, String> parameters = new HashMap<String, String>();
		parameters.put("accountId", accountId);
		parameters.put("applId", applId);
		SqlSession session = getMyBatisSession();
		return session.selectList("system.getAllObjectPermission", parameters);
	}

	/*
	 * (non-Javadoc)
	 * @see com.css.crm.dao.SystemDao#getAccountUnits(java.lang.String, java.lang.String)
	 */
	public String[] getAccountUnits(String accountId, String applId) {
		HashMap<String, String> parameters = new HashMap<String, String>();
		parameters.put("accountId", accountId);
		parameters.put("applId", applId);
		SqlSession session = getMyBatisSession();
		List<String> result = session.selectList("system.getAccountUnits", parameters);
		String[] rs = { "" };
		return result.toArray(rs);
	}

	/*
	 * (non-Javadoc)
	 * @see com.css.crm.dao.SystemDao#getAccountDefaultUnit(java.lang.String, java.lang.String)
	 */
	public String getAccountDefaultUnit(String accountId, String applId)throws SystemException {
		HashMap<String, String> parameters = new HashMap<String, String>();
		parameters.put("accountId", accountId);
		parameters.put("applId", applId);
		SqlSession session = getMyBatisSession();
		String accountUnit = session.selectOne("system.getAccountDefaultUnit", parameters);
		if (ConstantUtil.DB_EXCEPTION_CODE_0001.equals(accountUnit)) {
			throw new SystemException(SystemErrorMessage.SQL_MULTIPLY_RECORD);
		}
		return accountUnit;
	}

	/*
	 * (non-Javadoc)
	 * @see com.css.crm.dao.SystemDao#getAccountSingleUnit(java.lang.String, java.lang.String)
	 */
	public String getAccountSingleUnit(String accountId, String applId)throws SystemException {
		HashMap<String, String> parameters = new HashMap<String, String>();
		parameters.put("accountId", accountId);
		parameters.put("applId", applId);
		SqlSession session = getMyBatisSession();
		String accountUnit = session.selectOne("system.getAccountSingleUnit", parameters);
		if (ConstantUtil.DB_EXCEPTION_CODE_0001.equals(accountUnit)) {
			throw new SystemException(SystemErrorMessage.SQL_MULTIPLY_RECORD);
		}
		return accountUnit;
	}

	/*
	 * (non-Javadoc)
	 * @see com.css.crm.dao.SystemDao#getOrigCtryCode(java.lang.String)
	 */
	@Override
	public String getOrigCtryCode(String deptCde) {
		SqlSession session = getMyBatisSession();
		return session.selectOne("getOrigCtryCde", deptCde);
	}

	/*
	 * (non-Javadoc)
	 * @see com.css.crm.dao.SystemDao#getPlace(com.css.crm.vo.PlaceVo)
	 */
	@Override
	public List<PlaceVo> getPlace(PlaceVo place) {
		SqlSession session = getMyBatisSession();
		return session.selectList("system.getPlace", place);
	}

	/*
	 * (non-Javadoc)
	 * @see com.css.crm.dao.SystemDao#getDepartment(com.css.crm.vo.DepartmentVo)
	 */
	@Override
	public List<DepartmentVo> getDepartment(DepartmentVo department) {
		SqlSession session = getMyBatisSession();
		return session.selectList("system.getDepartment", department);
	}

	/*
	 * (non-Javadoc)
	 * @see com.css.crm.dao.SystemDao#getStaticReference(com.css.crm.vo.StaticReferenceVo)
	 */
	@Override
	public List<StaticReferenceVo> getStaticReference(StaticReferenceVo staticReference) {
		SqlSession session = getMyBatisSession();
		return session.selectList("system.getStaticReference", staticReference);
	}

	/*
	 * (non-Javadoc)
	 * @see com.css.crm.dao.SystemDao#queryPsDisc(com.css.crm.vo.DiscountCodeVo)
	 */
	@Override
	public List<DiscountCodeVo> getPsDisc(DiscountCodeVo vo) {
		SqlSession session = getMyBatisSession();
		return session.selectList("system.getPsDisc", vo);
	}

	/*
	 * (non-Javadoc)
	 * @see com.css.crm.dao.SystemDao#queryPsStatcRefExt(com.css.crm.vo.TitleVo)
	 */
	@Override
	public List<TitleVo> getPsStatcRefExt(TitleVo vo) {
		SqlSession session = getMyBatisSession();
		return session.selectList("system.getPsStatcRefExt", vo);
	}

	/*
	 * (non-Javadoc)
	 * @see com.css.crm.dao.SystemDao#getLdap(com.css.crm.vo.LdapVo)
	 */
	@Override
	public List<LdapVo> getLdap(LdapVo ldap) {
		SqlSession session = getMyBatisSession();
		return session.selectList("system.getLdap", ldap);
	}

	/*
	 * (non-Javadoc)
	 * @see com.css.crm.dao.SystemDao#querySmUser(com.css.crm.vo.SmUserVo)
	 */
	@Override
	public List<SmUserVo> getSmUser(SmUserVo vo) {
		SqlSession session = getMyBatisSession();
		return session.selectList("system.getSmUser", vo);
	}

	/*
	 * (non-Javadoc)
	 * @see com.css.crm.dao.SystemDao#getSmUserId(java.lang.String, java.lang.String)
	 */
	public String getSmUserId(String ldapId, String userUnitCde) throws SQLException {
		HashMap<String, Object> getSmUserByLdapPara = new HashMap<String, Object>();
		getSmUserByLdapPara.put("ldapId", ldapId);
		getSmUserByLdapPara.put("unitCde", userUnitCde);
		// String smUserId = (String)this.getSqlMapper().queryForObject("Common.getSmUserByLdap", getSmUserByLdapPara);
		SqlSession session = getMyBatisSession();
		String smUserId = session.selectOne("system.getSmUserByLdap", getSmUserByLdapPara);
		if (smUserId == null || smUserId.trim().isEmpty())
			throw new SQLException();
		return smUserId.trim();
	}
}
